<template>
  <div class="app-container">
  <div class="filter-container">
        <el-input clearable v-model="search.number" placeholder="合同号" style="width: 170px;margin-left:0px;"></el-input>
        <el-input clearable v-model="search.name" placeholder="销售顾问" style="width: 170px;margin-left:10px;"></el-input>
        <el-input clearable v-model="search.name" placeholder="具体车型" style="width: 170px;margin-left:10px;"></el-input>
        <el-date-picker v-model="search.daterange" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" style="margin-left:10px;">
        </el-date-picker>

        <el-button type="primary" icon="el-icon-search" @click="searchClick()" style="margin-left:10px;">查询</el-button>
    </div>
    <el-table v-loading="listLoading" :data="list" 
      border fit highlight-current-row stripe 
      style="width: 100%">
      <el-table-column align="center" label="序" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row._id }}</span>
        </template>
      </el-table-column>
     <el-table-column  min-width="100" align="center" label="订单合同号">
        <template slot-scope="scope">
          <span>{{ scope.row.ordernumber }}</span>
        </template>
      </el-table-column>
      <el-table-column  min-width="100" align="center" label="客户名称">
        <template slot-scope="scope">
          <span>{{ scope.row.customername }}</span>
        </template>
      </el-table-column>
      <el-table-column  min-width="100" align="center" label="具体车型">
        <template slot-scope="scope">
          <span>{{ scope.row.carname }}</span>
        </template>
      </el-table-column>
      <el-table-column  min-width="100" align="center" label="销售顾问">
        <template slot-scope="scope">
          <span>{{ scope.row.guider }}</span>
        </template>
      </el-table-column>
      <el-table-column  min-width="100" align="center" label="部门">
        <template slot-scope="scope">
          <span>{{ scope.row.depart }}</span>
        </template>
      </el-table-column>
      <el-table-column  min-width="100" align="center" label="状态">
        <template slot-scope="scope">
          <span>{{ scope.row.state | parseState }}</span>
        </template>
      </el-table-column>
      <el-table-column  min-width="100" align="center" label="日期">
        <template slot-scope="scope">
          <span>{{ scope.row.createtime | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      
      <el-table-column align="center" label="操作"  width="320" fixed="right">
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="tableRowClick().distribute(scope.row.id)">人工配车</el-button>
          <el-button type="primary" size="small" @click="tableRowClick().confirm(scope.row.id)">确认配车</el-button>
          <el-button type="primary" size="small" @click="tableRowClick().details(scope.row.id)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.rows" @pagination="fetchData" />

<!-- 人工配车 -->
<el-dialog :title="dialogDistribute.title" :visible.sync="dialogDistribute.visible"  width="900px">
    <div class="ci">
      <div class="item">
          <span class="name">销售合同号：</span>
          <span class="value">
              {{dialogDistribute.data.ordernumber}}
          </span>
      </div>
      <div class="item">
          <span class="name">具体车型：</span>
          <span class="value">
              {{dialogDistribute.data.carname}}
          </span>
      </div>
      <div class="item">
          <span class="name">系统默认VIN码：</span>
          <span class="value">
              无
          </span>
      </div>
      <div class="item">
          <span class="name">采购订单编号：</span>
          <span class="value">
              {{dialogDistribute.data.purchasenumber}}
          </span>
      </div>
      <div class="item">
          <span class="name">可配车辆选择：</span>
          <span class="value">
              <el-select v-model="dialogDistribute.data.carinfo" clearable placeholder="--" style="width: 220px;">
                    <el-option 
                    v-for="(item,index) in selectOptions.carinfo"
                    :key="index"
                    :label="item.label"
                    :value="item.value">
                    </el-option>
                </el-select>
          </span>
      </div>
      
      <div class="item">
          <span class="name">VIN码</span>
          <span class="value">
              <el-input disabled v-model="dialogDistribute.data.vincode" placeholder="" style="width:220px;" ></el-input>
          </span>
          <span class="name">采购订单编号</span>
          <span class="value">
              <el-input disabled v-model="dialogDistribute.data.purchasenumber" placeholder="" style="width:220px;" ></el-input>
          </span>
      </div>
      <div class="item">
          <span class="name">仓库名称</span>
          <span class="value">
              <el-input disabled v-model="dialogDistribute.data.storename" placeholder="" style="width:220px;" ></el-input>
          </span>
          <span class="name">仓位名称</span>
          <span class="value">
              <el-input disabled v-model="dialogDistribute.data.storeplace" placeholder="" style="width:220px;" ></el-input>
          </span>
      </div>

      
    </div>
    <div slot="footer" class="dialog-footer">
        <el-button @click="dialogDistribute.visible = false">取 消</el-button>
        <el-button type="primary" @click="dialogDistributeClick().save()" :loading="dialogDistribute.saveloading">保 存</el-button>
    </div>
</el-dialog>
<!-- end  人工配车 -->

<!-- 确认配车 -->
<el-dialog :title="dialogConfirm.title" :visible.sync="dialogConfirm.visible"  width="900px">
    <div class="ci">
      <div class="item">
          <span class="name">销售合同号：</span>
          <span class="value">
              {{dialogConfirm.data.salenumber}}
          </span>
      </div>
      <div class="item">
          <span class="name">具体车型：</span>
          <span class="value">
              {{dialogConfirm.data.carname}}
          </span>
      </div>
      <div class="item">
          <span class="name">系统默认VIN码：</span>
          <span class="value">
              {{dialogConfirm.data.systemvincode}}
          </span>
      </div>
      <div class="item">
          <span class="name">采购订单编号：</span>
          <span class="value">
              {{dialogConfirm.data.purchasenumber}}
          </span>
      </div>
      <div class="item">
          <span class="name">可配车辆选择：</span>
          <span class="value">
              <el-select v-model="dialogConfirm.data.carinfo" clearable placeholder="--" style="width: 220px;">
                    <el-option 
                    v-for="(item,index) in selectOptions.carinfo"
                    :key="index"
                    :label="item.label"
                    :value="item.value">
                    </el-option>
                </el-select>
          </span>
      </div>
      
      <div class="item">
          <span class="name">VIN码</span>
          <span class="value">
              <el-input disabled v-model="dialogConfirm.data.vincode" placeholder="" style="width:220px;" ></el-input>
          </span>
          <span class="name">采购订单编号</span>
          <span class="value">
              <el-input disabled v-model="dialogConfirm.data.purchasenumber" placeholder="" style="width:220px;" ></el-input>
          </span>
      </div>
      <div class="item">
          <span class="name">仓库名称</span>
          <span class="value">
              <el-input disabled v-model="dialogConfirm.data.storename" placeholder="" style="width:220px;" ></el-input>
          </span>
          <span class="name">仓位名称</span>
          <span class="value">
              <el-input disabled v-model="dialogConfirm.data.storeplace" placeholder="" style="width:220px;" ></el-input>
          </span>
      </div>

      
    </div>
    <div slot="footer" class="dialog-footer">
        <el-button @click="dialogConfirm.visible = false">取 消</el-button>
        <el-button type="primary" @click="dialogConfirmClick().save()" :loading="dialogConfirm.saveloading">保 存</el-button>
    </div>
</el-dialog>
<!-- end  确认配车 -->

  </div>
</template>

<script>
import * as carPurchaseApi from '@/api/cars/purchase'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import * as enumHelper from '@/utils/enum'
import {objectMerge,objectEmpty} from '@/utils/index'

export default {
    components: { Pagination },
    data(){
        return{
          search:{ 
              number:'',
              customer:'',
              phone:'',
              daterange:'',
          },
          listQuery: {
                page: 1,
                rows: 20,
                name:'',
                phone:'',
                state:-1,
                daterange:''
          },
          list:null,
          listLoading:false,
          total:0,
          selectOptions:{
            carinfo:[],
          },

          dialogDistribute:{
              visible:false,
              saveloading:false,
              title:'人工配车',
              data:{
                  id:'',
                  ordernumber:'',
                  carname:'',
                  systemvincode:'',
                  purchasenumber:'',
                  carinfo:'',
                  carname:'',
                  vincode:'',
                  storename:'',
                  storeplace:'',
              }
          },
          dialogConfirm:{
              visible:false,
              saveloading:false,
              title:'确认配车',
              data:{
                  id:'',
                  ordernumber:'',
                  carname:'',
                  systemvincode:'',
                  purchasenumber:'',
                  carinfo:'',
                  carname:'',
                  vincode:'',
                  storename:'',
                  storeplace:'',
              }
          },
          
        }
    },
    created(){
        var that=this;
        //初始化下拉菜单
        //enumHelper.initOptions(that.selectOptions.purchasetype,enumHelper.cars.purchaseType);
        //enumHelper.initOptions(that.selectOptions.paystage,enumHelper.cars.payStage);
        that.fetchData();
    },
    filters:{

      parseState(val){
        return enumHelper.cars.distributeState.getName(val);
      }

    },
    methods:{
        fetchData(){
          var that=this;
            carPurchaseApi.getDistributeList({}).then(res=>{
              if(res.data){
                that.list=[...res.data.items.map((item,index)=>{
                  item._id=index+1;
                  return item;
                })];
              }
            })
        },

        tableRowClick(){
          var that=this;
          return {
            distribute(id){
              var data=that.list.filter(o=>id==id)[0];
              that.dialogDistributeClick().init(data);
              that.dialogDistribute.visible=true;
            },
            confirm(id){
              that.dialogConfirmClick().init();
              that.dialogConfirm.visible=true;
            },
            details(id){
                that.$router.push({
                    path:'/cars/caroutstoredetails/'+id
                })
            }
          }
        },
        dialogDistributeClick(){
          var that=this;
          var dg=that.dialogDistribute;
          return{
            init(data){
              data?objectMerge(dg.data,data):objectEmpty(dg.data);
            },
            save(){
              dg.visible=false;
            }
          }
        },
        dialogConfirmClick(){
          var that=this;
          var dg=that.dialogConfirm;
          return{
            init(){
              objectEmpty(dg.data);
            },
            save(){
              dg.visible=false;
            }
          }
        }
    }
}
</script>

<style scoped>

</style>